package com.carrental.common.utils;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 订单编号生成工具类
 *
 * @author liangyi
 * @since 2022/3/10 14:13
 */
@Component
public class OrderNumberUtil {
    private final static String DATE_FORMAT = "yyyyMMdd";
    public String generateOrderNumber() {
        String prefix = getDate();
        String snowFlakeId = IdWorker.getIdStr();
        //截取雪花id后六位
        return prefix + snowFlakeId.substring(snowFlakeId.length() - 6);
    }
    private static String getDate() {
        //获取下单日期
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        return LocalDate.now().format(dateFormatter);
    }
}
